/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package basedatos;

import java.sql.Statement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import logica.cFormaPago;

/**
 *
 * @author Alberto
 */
public class GestorFormasPago {

    private static GestorFormasPago INSTANCE = null;
    private String user;
    private String password;
    private String url;

    private GestorFormasPago() {
        this.user = "root";
        this.password = "root";
        this.url = "jdbc:mysql://localhost:3306/repuestos_bd";
    }

    public static GestorFormasPago getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new GestorFormasPago();
        }
        return INSTANCE;
    }

    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (Exception e) {
            System.out.println("Error en la conexion");
        }
    }

    private Connection getConnection() throws SQLException {
        return DriverManager.getConnection(url, user, password);
    }

    public void insertarFormaPago(cFormaPago c) {
        String query = "INSERT INTO formas_pago (cod_forma_pago, forma_pago)"
                + "VALUES ('" + c.getCodFormaPago() + "', '" + c.getFormaPago() + "')";
//        System.out.println("insertarFormaPago() query=" + query);      
        Connection con = null;
        Statement pStmt = null;
        try {
            con = getConnection();
            pStmt = con.createStatement();
            pStmt.execute(query);

        } catch (Exception e) {
            /* CAPTURAR EXCEPTION */
        } finally {
            if (pStmt != null) {
                try {
                    pStmt.close();
                } catch (SQLException e) {
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                }
            }
        }
    }

    public void borrarFormaPago(cFormaPago c) throws SQLException {
        String query = "DELETE FROM formas_pago WHERE cod_forma_pago= '" + c.getCodFormaPago() + "'";
        Connection con = null;
        Statement pStmt = null;
        try {
            con = getConnection();
            pStmt = con.createStatement();
            pStmt.execute(query);

        } catch (Exception e) {
            /* CAPTURAR EXCEPTION */
        } finally {
            if (pStmt != null) {
                try {
                    pStmt.close();
                } catch (SQLException e) {
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                }
            }
        }
    }

    public void actulaizarFormaPago(cFormaPago c) throws SQLException {
        String query = "UPDATE formas_pago SET forma_pago= '" + c.getFormaPago()
                + "' where cod_forma_pago= '" + c.getCodFormaPago() + "'";
        Connection con = null;
        Statement pStmt = null;
        try {
            con = getConnection();
            pStmt = con.createStatement();
            pStmt.executeUpdate(query);

        } catch (Exception e) {
            /* CAPTURAR EXCEPTION */
        } finally {
            if (pStmt != null) {
                try {
                    pStmt.close();
                } catch (SQLException e) {
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                }
            }
        }
    }

    public Collection<cFormaPago> getFormaPago() {
        Collection<cFormaPago> formasPago = new ArrayList<cFormaPago>();
//        System.out.println("FormasPago "+formasPago.size());
        String query = "SELECT cod_forma_pago, forma_pago from formas_pago order by cod_forma_pago";
//        System.out.println(" QUERY: "+query);
        Connection con = null;
        Statement pStmt = null;
        try {
            con = getConnection();
            pStmt = con.createStatement();
            ResultSet set = pStmt.executeQuery(query);
            while (set.next()) {
                cFormaPago c = new cFormaPago();
                c.setCodFormaPago(set.getInt("cod_forma_pago"));
                c.setFormaPago(set.getString("forma_pago"));
                formasPago.add(c);
            }
        } catch (Exception e) {
            System.out.println("ERROR: " + e.getMessage());
            /* CAPTURAR EXCEPTION */
        } finally {
            if (pStmt != null) {
                try {
                    pStmt.close();
                } catch (SQLException e) {
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                }
            }
            return formasPago;
        }
    }

    public boolean existeFormaPago(int codFormaPago) {
        String query = "SELECT * from formas_pago where cod_forma_pago = '" + codFormaPago + "'";
        Connection con = null;
        Statement pStmt = null;
        try {
            con = getConnection();
            pStmt = con.createStatement();
            ResultSet set = pStmt.executeQuery(query);
            if (set.next()) {
                return true;
            }
            return false;
        } catch (Exception e) {
            return false;
        } finally {
            if (pStmt != null) {
                try {
                    pStmt.close();
                } catch (SQLException e) {
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                }
            }
        }
    }
}